package com.thti.mecool.client.command.result.plan;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.threeti.mecool.core.domain.model.plan.pojo.Plan;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanJob;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanProject;
import com.threeti.mecool.core.domain.model.plan.pojo.Step;
import com.threeti.mecool.core.domain.model.plan.pojo.StepItem;
import com.threeti.mecool.core.domain.model.plan.pojo.TaskItem;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.protocol.plan.PlansProtocolResult;
import com.thti.mecool.client.protocol.plan.Sales;
import com.thti.mecool.client.service.Protocol;
import com.thti.mecool.client.util.TimeUtil;
import com.thti.mecool.client.util.Util;

public class PlanCommand implements ICommand {
	PlanBizImpl planBizImpl;
	List<Map<String, Object[]>> list = new ArrayList<Map<String, Object[]>>();

	@Override
	public void doCommand(Object o) {
		PlansProtocolResult plansProtocolResult = (PlansProtocolResult) o;
		if (plansProtocolResult != null) {
			if (plansProtocolResult.getStatus() == 1) {
				List<Plan> plans = plansProtocolResult.getPlans();
				if (plans.size() > 0 && plans != null) {
					for (int n = 0; n < plans.size(); n++) {
						planBizImpl = new PlanBizImpl(Cache.getmContext());
						Plan planServer = plans.get(n);
						if (null != planServer) { 
							String planId=String.valueOf(planServer.getId());
							Util.print("planId:  "+(!planBizImpl.findByPlanId(planId,BaseActivity.getCurrentUser().getId())));
							if (!planBizImpl.findByPlanId(planId,BaseActivity.getCurrentUser().getId())) {
								list.add(savePlan(planServer));
							}
							List<PlanProject> planProjects = planServer
									.getPlanProjects();
							if (planProjects.size() > 0) {
								String proId=null;
								for (int i = 0; i < planProjects.size(); i++) {
									if(proId==null || !proId.equals(planProjects.get(i).getProjectCode())){
										
										if(!planBizImpl.findByProjectId(planId, planProjects.get(i).getProjectCode(), BaseActivity.getCurrentUser().getId())){
										 list.add(saveProject(planProjects.get(i)));
										}
									}
									proId=planProjects.get(i).getProjectCode();

									List<PlanJob> planJobs = planProjects
											.get(i).getPlanJobs();
									if (planJobs.size() > 0) {
										for (int j = 0; j < planJobs.size(); j++) {
											
											List<Step> steps = planJobs.get(j)
													.getJob().getSteps();
											List<Sales> listSales=planJobs.get(j).getSales();
											if(listSales!=null && listSales.size()>0){
												if(!planBizImpl.findByTaskId(planId, proId, String.valueOf(planJobs.get(j).getId()), BaseActivity.getCurrentUser().getId())){
													list.add(saveTask(planJobs.get(j),
															planServer.getType()));
												
												for (int m = 0; m < listSales.size(); m++) {
													
													list.add(saveSalesStep(
															listSales.get(m),
															null,
															String.valueOf(planProjects
																	.get(i)
																	.getProjectCode()),
															String.valueOf(planServer
																	.getId()),
																	planJobs.get(j).getStoreAddress(),
															0,
															String.valueOf(planJobs
																	.get(j)
																	.getId()),
															String.valueOf(planJobs
																	.get(j)
																	.getId()),0));
													for (int k = 0; k < steps.size(); k++) {
														list.add(saveTaskStep(
																steps.get(k),
																null,
																String.valueOf(planProjects
																		.get(i)
																		.getProjectCode()),
																String.valueOf(planServer
																		.getId()),
																		planJobs.get(j).getStoreAddress(),
																3,
																listSales.get(m).getSalesCode(),
																String.valueOf(planJobs.get(j).getId())
																,0));
														List<TaskItem> taskItems =steps.get(k).getTask().getTaskItems();
														if(taskItems!=null & taskItems.size()>0){
															for (int l = 0; l < taskItems.size(); l++) {
																list.add(saveTaskItem(listSales.get(m).getSalesCode(),taskItems.get(l), String.valueOf(planProjects
																		.get(i).getProjectCode()), String.valueOf(planServer
																		.getId()), planJobs.get(j).getStoreAddress(), 2, String.valueOf(steps.get(k).getId()),
																		String.valueOf(planJobs.get(j).getId()), 0));
															}
														}
													}
												}
												}
											}else if(planJobs.get(j).getHasScoreTask()!=1){
												if(!planBizImpl.findByTaskId(planId, proId, String.valueOf(planJobs.get(j).getId()), BaseActivity.getCurrentUser().getId())){
													list.add(saveTask(planJobs.get(j),
															planServer.getType()));
											
												if (steps.size() > 0) {
													System.out
															.println("steps>0");
													for (int k = 0; k < steps
															.size(); k++) {
														List<StepItem> stepItems = steps
																.get(k)
																.getStepItems();
														if (stepItems != null
																&& stepItems.size() > 0) {

															list.add(saveTaskStep(
																	steps.get(k),
																	null,
																	String.valueOf(planProjects
																			.get(i)
																			.getProjectCode()),
																	String.valueOf(planServer
																			.getId()),
																			planJobs.get(j).getStoreAddress(),
																	0,
																	String.valueOf(planJobs
																			.get(j)
																			.getId()),
																	String.valueOf(planJobs
																			.get(j)
																			.getId()),0));
															SaveStepItem(
																	stepItems,
																	String.valueOf(planProjects
																			.get(i)
																			.getProjectCode()),
																	String.valueOf(planServer
																			.getId()),planJobs.get(j).getStoreAddress(),
																	String.valueOf(planJobs
																			.get(j)
																			.getId()));
														}else{
															list.add(saveTaskStep(
																	steps.get(k),
																	null,
																	String.valueOf(planProjects
																			.get(i)
																			.getProjectCode()),
																	String.valueOf(planServer
																			.getId()),planJobs.get(j).getStoreAddress(),
																	0,
																	String.valueOf(planJobs
																			.get(j)
																			.getId()),
																	String.valueOf(planJobs
																			.get(j)
																			.getId()),1));
														}}
													}
												}
											}
										}}
									}
								
								}
							}
						}
					}
					if (null != list && list.size() > 0) {
						planBizImpl.savePlan(list);
					}
				}
				BaseActivity.setDataFlag(HandlerConstant.PlanCommand);
			}else{
				BaseActivity.setFailName("任务管理");
				BaseActivity.setDataFlag(HandlerConstant.CommandFail);
			}
	}

	public void SaveStepItem(List<StepItem> stepItems, String projectId,
			String planId,String address, String jobId) {
		if (stepItems.size() > 0 && stepItems != null) {
			for (int i = 0; i < stepItems.size(); i++) {
				List<Step> steps = stepItems.get(i).getSteps();
				if (steps.size() > 0 && steps != null) {
					list.add(saveTaskStep(null, stepItems.get(i), projectId,
							planId,address, 2,
							String.valueOf(stepItems.get(i).getStep().getId()),
							jobId,0));
					for (int j = 0; j < steps.size(); j++) {
						
						List<StepItem> stepItemss = steps.get(j).getStepItems();
						if (stepItemss.size() > 0 && stepItemss != null) {
							list.add(saveTaskStep(
									steps.get(j),
									null,
									projectId,
									planId,address,
									1,
									String.valueOf(steps.get(j).getStepItem()
											.getId()), jobId,0));
							SaveStepItem(stepItemss, projectId, planId,address, jobId);
						}else{
							list.add(saveTaskStep(
									steps.get(j),
									null,
									projectId,
									planId,address,
									1,
									String.valueOf(steps.get(j).getStepItem()
											.getId()), jobId,2));
						}
					}
				}else{
					list.add(saveTaskStep(null, stepItems.get(i), projectId,
							planId,address,2,
							String.valueOf(stepItems.get(i).getStep().getId()),
							jobId,1));
				}
			}
		}
	}

	public Map<String, Object[]> savePlan(Plan planServer) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		map.put(HandlerConstant.PLAN_SQL,
				new Object[] { BaseActivity.getCurrentUser().getId(),
						planServer.getId(), planServer.getName(), 0, 1,
						TimeUtil.dateToYYYYMM(planServer.getPlanDate()),
						planServer.getStatus(), planServer.getDenyNotes(),
						planServer.getChangeNotes(), planServer.getType(),
						new Date() });
		return map;
	}

	public Map<String, Object[]> saveProject(PlanProject planProject) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		map.put(HandlerConstant.PROJECT_SQL,
				new Object[] { BaseActivity.getCurrentUser().getId(),
						planProject.getProjectCode(),
						planProject.getProjectName(),
						planProject.getPlan().getId(), "1", new Date() });
		return map;
	}

	public Map<String, Object[]> saveTask(PlanJob planJob, String type) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		map.put(HandlerConstant.TASK_SQL,
				new Object[] { BaseActivity.getCurrentUser().getId(),
						planJob.getPlanProject().getProjectCode(),
						planJob.getId(), planJob.getJob().getName(),
						TimeUtil.dateToString(planJob.getStartTime(), 1),
						TimeUtil.dateToString(planJob.getEndTime(), 1),
						planJob.getStoreCode(), planJob.getStoreName(),
						planJob.getPlanProject().getPlan().getId(), type ,0});
		Util.print(planJob.getJob().getName()+"       :     "+planJob.getStoreName());
		Util.print("planJob.getStartTime()   :   "+planJob.getStartTime());
		Util.print("planJob.getEndTime()     :  "+planJob.getEndTime());
		return map;
	}
	
	public Map<String, Object[]> saveSalesStep(Sales sales, StepItem stepItem,
			String projectId, String planId,String address, int isType, String parentId,
			String jobId,int isOver) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
			map.put(HandlerConstant.TASK_STEP_SQL, new Object[] { planId,
					projectId, jobId, sales.getSalesCode(), parentId, isType,
                    6, sales.getSalesName(),address ,null,null,null, isOver});
		return map;
	}
	
	public Map<String, Object[]> saveTaskItem(String saleCode, TaskItem tasktem,
			String projectId, String planId,String address, int isType, String parentId,
			String jobId,int isOver) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
			map.put(HandlerConstant.TASKITEM_STEP_SQL, new Object[] { planId,
					projectId, jobId, String.valueOf(tasktem.getId()), parentId, isType,
                    -2, tasktem.getName(),address ,String.valueOf(tasktem.getId()), saleCode ,null,tasktem.getExtra2(),null, isOver});
		return map;
	}

	public Map<String, Object[]> saveTaskStep(Step step, StepItem stepItem,
			String projectId, String planId,String address, int isType, String parentId,
			String jobId,int isOver) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		if (isType != 2) {

			map.put(HandlerConstant.TASK_STEP_SQL, new Object[] { planId,
					projectId, jobId, step.getId(), parentId, isType,
                    (step.getTask().getActionId()==4&&step.getCheckBox()==1)?5:step.getTask().getActionId(),
                    step.getName(),address ,null,null,null, isOver});
		} else {
			map.put(HandlerConstant.TASK_STEP_SQL, new Object[] { planId,
					projectId, jobId, stepItem.getId(), parentId, isType,"-1",
					stepItem.getTaskItem().getName(),address,stepItem.getTaskItem().getExtra1(),stepItem.getTaskItem().getExtra2()
                    ,stepItem.getTaskItem().getExtra3(),isOver});
		}
		return map;
	}

	@Override
	public String getCommandID() {
		// TODO Auto-generated method stub
		return Protocol.getPlan;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return PlansProtocolResult.class;
	}

}
